package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.dto.order.OrderStatisticsDto;
import com.atguigu.spzx.model.entity.order.OrderInfo;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import com.atguigu.spzx.model.vo.order.OrderStatisticsVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.spzx.manager.service.OrderInfoService;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Windows
 * @description 针对表【order_info(订单)】的数据库操作Service实现
 * @createDate 2024-02-01 16:29:38
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo>
        implements OrderInfoService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectList(new LambdaQueryWrapper<OrderStatistics>()
                .ge(StringUtils.isNotBlank(orderStatisticsDto.getCreateTimeBegin()),OrderStatistics::getOrderDate, orderStatisticsDto.getCreateTimeBegin())
                .le(StringUtils.isNotBlank(orderStatisticsDto.getCreateTimeEnd()),OrderStatistics::getOrderDate, orderStatisticsDto.getCreateTimeEnd()));

        List<String> orderDateList = orderStatisticsList.stream().map(orderStatistics ->
                DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd")
        ).collect(Collectors.toList());

        List<BigDecimal> totalAmountList = orderStatisticsList
                .stream().map(OrderStatistics::getTotalAmount).collect(Collectors.toList());

        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(orderDateList);
        orderStatisticsVo.setAmountList(totalAmountList);

        return orderStatisticsVo;
    }
}




